package thread.java20230110;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 使用Callable 来计算1..1000相加
 */
public class Demo50 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable callable = () -> {
            int sum = 0;
            for (int i = 1; i <=1000 ; i++) {
                sum += i;
            }
            return sum;
        };
        //使用FutureTask把Callable 包装, 交给线程执行
        FutureTask<Integer> task = new FutureTask(callable);
        new Thread(task).start();
        //获取线程的执行结果
        int sum = task.get();
        System.out.println(sum);

    }
}
